﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System.Collections.Generic;

namespace Tesla.Collections {
    /// <summary>
    /// A Dictionary that manages its own read-only version of itself,
    /// rather than creating a new one each time AsReadonly() is called. The
    /// read-only version is only created when first requested, and uses the existing data
    /// to read from.
    /// 
    /// This is useful if you want the function of a Dictionary, but
    /// be able to limit it to read-only outside of its manager/owner.
    /// </summary>
    /// <typeparam name="TKey">The type of keys in the collection.</typeparam>
    /// <typeparam name="TValue">The type of values in the collection.</typeparam>
    public sealed class TeslaDictionary<TKey, TValue> : Dictionary<TKey, TValue> {
        private ReadOnlyDictionary<TKey, TValue> _readOnlyDictionary;

        /// <summary>
        /// Creates a new instance of <see cref="TeslaDictionary&lt;TKey, TValue&gt;"/>.
        /// </summary>
        public TeslaDictionary()
            : base() {
                _readOnlyDictionary = new ReadOnlyDictionary<TKey, TValue>(this);
        }

        /// <summary>
        /// Creates a new instance of <see cref="TeslaDictionary&lt;TKey, TValue&gt;"/>.
        /// </summary>
        /// <param name="capacity">The initial dictionary capacity.</param>
        public TeslaDictionary(int capacity)
            : base(capacity) {
                _readOnlyDictionary = new ReadOnlyDictionary<TKey, TValue>(this);
        }

        /// <summary>
        /// Creates a new instance of <see cref="TeslaDictionary&lt;TKey, TValue&gt;"/>.
        /// </summary>
        /// <param name="comparater">The comparater.</param>
        public TeslaDictionary(IEqualityComparer<TKey> comparater)
            : base(comparater) {
                _readOnlyDictionary = new ReadOnlyDictionary<TKey, TValue>(this);
        }

        /// <summary>
        /// Returns a read-only version of this dictionary.
        /// </summary>
        /// <returns>Read-only dictionary</returns>
        public ReadOnlyDictionary<TKey, TValue> AsReadOnly() {
            return _readOnlyDictionary;
        }
    }
}
